/***************************************************************************
 *   Copyright (C) 2007 by Grzegorz Latarowki, Maciej Gajewski             *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
 ***************************************************************************/

// libc
#include <stdio.h>

// Qt
#include <QCoreApplication>
#include <QThread>
#include <QTcpServer>
#include <QTcpSocket>

// libautonet
#include "autopackets.h"

// local
#include "gexception.h"
#include "serverlistener.h"
#include "serverthread.h"
#include "eventserver.h"
#include "constants.h"




// ----------------------- AdjusterServerThread{} ----------------------------
// ---------------------------------------------------------------------------
class AdjusterServerThread : public QThread
{
public:
	AdjusterServerThread(){}
	virtual ~AdjusterServerThread(){}
	
	virtual void run()
	{
		try
		{
			// create server listener
			ServerListener serverListener( ServerThread::SERVER_ADJUSTER );
	
			// listen, process
			qDebug("Starting Adjuster server: %s %s", __DATE__, __TIME__ );
			serverListener.listen( QHostAddress::Any, ADJUSTER_SERVER_PORT );
		}
		catch( GException& e )
		{
			qDebug("server error: %s", (const char*)e.getMessage().toAscii() );
		}
	}
}; // class !AdjusterServerThread
// ---------------------------------------------------------------------------




// --------------------------- BaseServerThread{} ----------------------------
// ---------------------------------------------------------------------------
class BaseServerThread : public QThread
{
public:
	BaseServerThread(){}
	virtual ~BaseServerThread(){}
	
	virtual void run()
	{
		try
		{
			QString interface( "0.0.0.0" );
			// load interface to listen on
			QSettings settings( SETTINGS_FILE, QSettings::IniFormat );
			if ( settings.status() == QSettings::NoError )
			{
				// load settings
				settings.beginGroup("BaseServerInterface"); // TODO use defined const
				interface = settings.value( "interface", interface ).toString();
				
				// re-write settings
				settings.setValue( "interface", interface );
			}
			else
			{
				qWarning("Can not read settings file %s", SETTINGS_FILE );
			}

			
			// create server listener
			ServerListener serverListener( ServerThread::SERVER_BASE );
	
			// listen, process
			qDebug("Starting Base server: %s %s", __DATE__, __TIME__ );
			serverListener.listen( QHostAddress( interface ), BASE_SERVER_PORT );
		}
		catch( GException& e )
		{
			qDebug("server error: %s", (const char*)e.getMessage().toAscii() );
		}
	}
}; // class !AdjusterServerThread
// ---------------------------------------------------------------------------


// ---------------------------- run ------------------------------------------
int main( int argc, char** argv )
{
	QCoreApplication app( argc, argv );
	
	try
	{
		AdjusterServerThread adjusterServerThread;
		BaseServerThread     baseServerThread;
		EventServer          eventServer;

			// start in background 
		adjusterServerThread.start();
		baseServerThread.start();

			// start in foreground 
		eventServer.run();
	}
	catch ( const GException& e )
	{
		qDebug("Error: %s", qPrintable( e.getMessage() ) );
	}
	catch( ... )
	{
		qDebug("unknown error");
	}
	return 0;
}
// ---------------------------------------------------------------------------



// eof

